package xjtu.sad.zbsj.task.annotater.manageTask.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xjtu.sad.zbsj.common.PageParam;
import xjtu.sad.zbsj.dao.labelTask.LabelTaskDao;
import xjtu.sad.zbsj.dao.manageTask.ManageTaskDao;
import xjtu.sad.zbsj.task.annotater.labelTask.TaskInfo;
import xjtu.sad.zbsj.task.annotater.manageTask.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ManageTaskServiceImpl implements ManageTaskService {
    @Resource
    private ManageTaskDao manageTaskDao;

    @Resource
    private LabelTaskDao labelTaskDao;

    @Override
    public Page<ManageTask> getTaskList(PageParam pageParam) {
        //分页
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<ManageTask> taskList= manageTaskDao.getTaskList();
        Page<ManageTask> pageTaskList=(Page<ManageTask>)  taskList;
        return pageTaskList;
    }

    @Override
    public Page<ManageTask> getMyTask(Integer userId,PageParam pageParam) {

        //分页
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<ManageTask> manageTaskList = manageTaskDao.getMyTask(userId);
        for (ManageTask manageTask : manageTaskList) {
            Date start_time = manageTask.getStart_time();
            Calendar c = Calendar.getInstance();
            c.setTime(start_time);
            c.add(Calendar.HOUR, manageTask.getTask_limit_time()); //利用Calendar 实现 Date日期+1天
            manageTask.setExpect_end_time(c.getTime());
        }
        Page<ManageTask> pageMyTaskList=(Page<ManageTask>)  manageTaskList;
        return pageMyTaskList;
    }

    @Override
    public Page<TaskMark> getMarkDetail(Integer draw_id,PageParam pageParam){
        //分页
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<TaskMark> taskMarkList = manageTaskDao.getDetail(draw_id);
        System.out.println(taskMarkList);
        Page<TaskMark> pageMarkList=(Page<TaskMark>)  taskMarkList;
        return pageMarkList;
    }


    @Override
    public String drawTask(DrawTask drawTask, Integer userId) {

        Integer task_max_distribute = manageTaskDao.getTaskMaxDistribute(drawTask.getTask_id());
        Integer task_mark_count = manageTaskDao.getTask_mark_count(drawTask.getTask_id());
        Integer alreadyGet = 0;    //已领取到的数量
        Integer times = 0;

        List<Integer> userMarkList = manageTaskDao.getUserMarkList(userId, drawTask.getTask_id());// 获得当前用户已标注列表
        TaskInfo taskInfo = labelTaskDao.getTaskInfo(drawTask.getTask_id());// 获得当前任务信息

        // 判断该任务是否已被发完
        if(manageTaskDao.drawNum(taskInfo.getTaskId())<taskInfo.getContentCount()*taskInfo.getMarkCount()){
            // 判断该用户是否已标注完该任务所有样本
            if (userMarkList.size() == taskInfo.getContentCount()) {
                return "您已完成该任务所有标注";
            }
            manageTaskDao.insertDrawTaskIni(drawTask.getTask_id(), drawTask.getStart_time(), userId);   //一次领取插入
            Integer TaskDrawId = manageTaskDao.selectTaskDrawId(drawTask.getTask_id(), userId, drawTask.getStart_time()); //领取id

            while (times < task_mark_count) {
                // 获得当前用户可领取的分发次数为times的列表
                List<DrawContent> drawContentList = manageTaskDao.getDrawDistributeTimes(drawTask.getTask_id(), times, userMarkList, userMarkList.size());
                System.out.println("drawContentList" + drawContentList.size() + drawContentList);
                if (drawContentList.size() >= task_max_distribute - alreadyGet) {  //领取次数=times的总剩余量够
                    List<DrawContent> insertContentList = new ArrayList<>();
                    for (int i = 0; i < task_max_distribute - alreadyGet; i++) {
                        DrawContent drawContent = drawContentList.get(i);
                        insertContentList.add(drawContent);
                    }

                    System.out.println(insertContentList.size());
                    System.out.println("insertMark" + insertContentList.size() + insertContentList.size());
                    manageTaskDao.insertMark(drawTask.getTask_id(), userId, TaskDrawId, insertContentList);
                    manageTaskDao.updateDistributeTimes(insertContentList, times + 1);

                    alreadyGet = task_max_distribute;
                    manageTaskDao.updateDrawTaskTimes(TaskDrawId, alreadyGet);

                    break; //已经领取到最大分配数量
                } else {  //领取次数=times的总剩余量不够
                    System.out.println(drawContentList);
                    if (drawContentList.size() != 0) {
                        manageTaskDao.insertMark(drawTask.getTask_id(), userId, TaskDrawId, drawContentList);//连接mark表和task_content表
                        times++;
                        manageTaskDao.updateDistributeTimes(drawContentList, times);//将领取次数为times的题都分给此用户，并修改分发次数
                        alreadyGet += drawContentList.size();
                        userMarkList = manageTaskDao.getUserMarkList(userId, drawTask.getTask_id());// 获得当前用户已标注列表
                    } else {
                        times++;
                    }
                }

            }
            Integer distributeCount = manageTaskDao.getDistributeCount(drawTask.getTask_id());// 查询当前任务总共被标注了多少次,算每个样本被重复标注的

            // 当前任务被标注的次数等于当前任务最大被分发数量则修改任务状态为3(分配完成)
            if (distributeCount == taskInfo.getContentCount() * taskInfo.getMarkCount()) {
                manageTaskDao.updateTaskStatus(drawTask.getTask_id());// 更新任务状态
            }

            if (alreadyGet < task_max_distribute) {
                System.out.println("任务数量不足，以为您领取最大剩余数量");
                manageTaskDao.updateDrawTaskTimes(TaskDrawId, alreadyGet);
                return "任务数量不足，以为您领取最大剩余数量";
            }
            return "领取成功";
        }
        else return "该任务已被领取完";
    }

    /**
     * 判断该用户是否可以领取此任务,即判断他是否将领取的任务标注完成
     *
     * @param userId  用户id
     * @param task_id 任务id
     * @return 是否允许领取
     */
    @Override
    public boolean judgeAllowDraw(Integer userId, Integer task_id) {
        List<DrawContent> drawContentList = manageTaskDao.getDrawContentList(userId, task_id);
        for (DrawContent drawContent : drawContentList) {
            if ("WWC".equals(drawContent.getDraw_status())) {
                return false;
            }
        }
        return true;
    }
}





